package com.xiaotou.struts.action;

import java.util.List;
import java.util.Set;

import javax.servlet.ServletContext;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;
import com.xiaotou.db.pojo.ActionTask;
import com.xiaotou.db.pojo.WbUser;
import com.xiaotou.domain.AvailableItem;
import com.xiaotou.service.ActionService;
import com.xiaotou.utils.AuthUtils;
import com.xiaotou.weibo.WeiboClient;
import com.xiaotou.weibo.WeiboClientException;
import com.xiaotou.weibo.WeiboFactory;
import com.xiaotou.weibo.WeiboUser;

public class ActionAction extends ActionSupport implements ServletContextAware {
	private static final long serialVersionUID = 6144511066951012811L;

	private ActionService actionService;

	private ServletContext servletContext;

	private Long preyId;

	private Long actionId;

	private List<WeiboUser> follows;

	private ActionTask actionTask;

	private Set<AvailableItem> items;

	private int gotMoney;

	@Action(value = "/gathering", results = { @Result(name = "success", location = "/WEB-INF/jsp/gathering.jsp") })
	public String gathering() throws WeiboClientException {
		final WbUser wbUser = AuthUtils.getLoginUser();
		this.actionTask = this.actionService.gathering(wbUser, preyId);
		final WeiboClient weiboClient = WeiboFactory.createWeiboClient(wbUser);
		this.follows = weiboClient.getFollows(wbUser.getWbId().toString());

		return SUCCESS;
	}

	@Action(value = "/actionItems", results = { @Result(name = "success", type = "json", params = {
			"root", "items" }) })
	public String actionItems() {
		items = this.actionService.getItems(this.actionId);
		return SUCCESS;
	}

	@Action(value = "/start", results = { @Result(name = "success", location = "/WEB-INF/jsp/start.jsp") })
	public String start() {
		this.actionTask = this.actionService.getActionTask(actionId);
		if (this.actionTask.getActionTime() == null) {
			this.actionTask = this.actionService.start(actionId);
		}
		return SUCCESS;
	}

	@Action(value = "/done", results = { @Result(name = "success", location = "/WEB-INF/jsp/done.jsp") })
	public String done() {
		this.actionTask = this.actionService.getActionTask(actionId);
		if (this.actionTask.getDoneTime() == null) {
			this.actionTask = this.actionService.done(actionId, gotMoney);
		}
		return SUCCESS;
	}

	@Action(value = "/snapshot", results = { @Result(name = "success", location = "/WEB-INF/jsp/snapshot.jsp") })
	public String steal() {
		this.actionTask = this.actionService.getActionTask(actionId);
		if (actionTask.getSnapshot() == null) {
			actionTask = actionService.snapshot(actionId,
					this.servletContext.getRealPath("/"));
		}
		return SUCCESS;
	}

	public void setActionService(ActionService actionService) {
		this.actionService = actionService;
	}

	public List<WeiboUser> getFollows() {
		return follows;
	}

	public void setPreyId(Long preyId) {
		this.preyId = preyId;
	}

	public ActionTask getActionTask() {
		return actionTask;
	}

	public void setActionId(Long actionId) {
		this.actionId = actionId;
	}

	public void setServletContext(ServletContext context) {
		this.servletContext = context;
	}

	public void setGotMoney(int gotMoney) {
		this.gotMoney = gotMoney;
	}

	public Set<AvailableItem> getItems() {
		return items;
	}
}
